package com.reyco1.medusa.display.control
{
	import com.reyco1.medusa.display.SmartSprite;
	
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class Spinner extends BaseControl
	{
		private var container:SmartSprite;
		
		private var _timer:Timer;
		private var _slices:uint;
		private var _radius:uint;
		private var _color:uint;
		private var _speed:uint;
		
		public function Spinner(slices:uint = 12, radius:uint = 4, color:uint = 0xFF0000, speed:uint = 65)
		{
			super();
			
			_slices = slices;
			_radius = radius;
			_color  = color;
			_speed  = speed;
			
			build();
		}
		
		override protected function onAdded():void
		{
			_timer = new Timer(speed);
			_timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
			_timer.start();
		}
		
		override protected function onRemoved():void
		{
			_timer.reset();
			_timer.removeEventListener(TimerEvent.TIMER, onTimer);
			_timer = null;
			
			removeAllChildren();
		}
		
		override protected function build():void
		{
			if(container)
			{
				container.removeAllChildren();
				removeChild(container);
			}
			
			container = new SmartSprite();
			addChild(container);
			
			var i:uint = slices;
			var degrees:uint = 360 / slices;
			while (i--)
			{
				var slice:Shape = createSlice();
				var radianAngle:Number = (degrees * i) * Math.PI / 180;
				slice.alpha = Math.max(0.2, 1 - (0.1 * i));
				slice.rotation = -degrees * i;
				slice.x = Math.sin(radianAngle) * radius;
				slice.y = Math.cos(radianAngle) * radius;
				container.addChild(slice);
			}
			
			container.x = container.width * 0.5;
			container.y = container.height * 0.5;
			
			_width = container.width;
			_height = container.height;
			
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function createSlice():Shape
		{
			var slice:Shape = new Shape();
			slice.graphics.beginFill(color);
			slice.graphics.drawRoundRect(-1, 0, 2, (radius + 2), 12, 12);
			slice.graphics.endFill();
			return slice;
		}
		
		public function get timer():Timer
		{
			return _timer;
		}
		
		public function set timer(value:Timer):void
		{
			_timer = value;
			invalidate();
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		public function set color(value:uint):void
		{
			_color = value;
			invalidate();
		}
		
		public function get radius():uint
		{
			return _radius;
		}
		
		public function set radius(value:uint):void
		{
			_radius = value;
			invalidate();
		}
		
		public function get slices():uint
		{
			return _slices;
		}
		
		public function set slices(value:uint):void
		{
			_slices = value;
			invalidate();
		}
		
		public function get speed():uint
		{
			return _speed;
		}
		
		public function set speed(value:uint):void
		{
			_speed = value;
			invalidate();
		}
		
		private function onTimer(e:TimerEvent):void
		{
			container.rotation = (container.rotation + (360 / slices)) % 360;
		}
	}
}